//
//  SancanViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SancanViewController.h"
#import "ShikeViewController.h"
@interface SancanViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;//解析数据的数组
@property(nonatomic ,retain)UITableView *tableView;
@property(nonatomic, assign)BOOL isRefrsh;
@end
static NSInteger  i = 0;
static NSInteger j = 6;
@implementation SancanViewController

- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view .backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.s_model.text;
    self.dataArr = [ NSMutableArray array];
    [self prepareData];
    [self creatView];
    [self loadMBProgress];
    [self.tableView.mj_header  beginRefreshing];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];

}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)prepareData
{
    NSString *str = [NSString stringWithFormat:@"http://api.izhangchu.com/?cat_id=%@&methodName=CategorySearch&page=%ld&size=%ld&token=0&type=1&user_id=0&version=4.1",self.s_model.s_id,i,j];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (_isRefrsh) {
            [_dataArr removeAllObjects];
        }
        NSArray *arr = responseObject[@"data"][@"data"];
        [self.dataArr addObjectsFromArray:[ ShipuModel modelHanderWithArray:arr]];
        NSLog(@"%@",_dataArr);
        [_tableView reloadData];
        [self.hud removeFromSuperview];
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView.mj_footer endRefreshing];
        
           }
    
    
   
    failure:^(NSURLSessionDataTask *task, NSError *error) {
                           
                       }];
}

-(void)creatView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _tableView.backgroundColor = [ UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[SancanTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];

    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
            i = 1;
        self.isRefrsh = YES;
        
        [self  prepareData];
    }];
    self.tableView.mj_header = header;

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        j += 6;
        self.isRefrsh = NO;
        [self prepareData];
    }];
    self.tableView.mj_footer = footer;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SancanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ShipuModel *m = _dataArr[ indexPath.row];
    [cell.img sd_setImageWithURL: [ NSURL URLWithString:m.image]];
    cell.name.text = m.title;
    cell.content.text = m.des;
    return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230 *HEIGHT;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ShikeViewController *shike = [[ ShikeViewController alloc] init];
    shike.model = _dataArr[indexPath.row];
    [self.navigationController pushViewController:shike animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
